package zzs.generalframework.util.core.calendar;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @title
 * @description
 * @project zzs-util
 * @package zzs.generalframework.util.core.calendar
 * @author zzs
 * @date 2018年1月30日
 * @version 1.0
 */
public class CalendarUtils
{

	private static final String dateTimePattern = "yyyy-MM-dd HH:mm:ss";
	private static final String dateTimePattern2 = "yyyyMMdd HH:mm:ss";
	private static final String datePattern = "yyyy-MM-dd";
	private static final String datePattern_yyyyMMDD = "yyyyMMdd";

	/**
	 * 获取当前年份
	 * 
	 * @return
	 */
	public static int getYear()
	{
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 获取年份
	 * 
	 * @param calendar
	 * @return
	 */
	public static int getYear(Calendar calendar)
	{
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 获取月份
	 * 
	 * @return
	 */
	public static int getMonth()
	{
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取月份
	 * 
	 * @param calendar
	 * @return
	 */
	public static int getMonth(Calendar calendar)
	{
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取日期
	 * 
	 * @return
	 */
	public static int getDay()
	{
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取日期
	 * 
	 * @param calendar
	 * @return
	 */
	public static int getDay(Calendar calendar)
	{
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取星期几
	 * 
	 * @return
	 */
	public static int getWeek()
	{
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 获取星期几
	 * 
	 * @param calendar
	 * @return
	 */
	public static int getWeek(Calendar calendar)
	{
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 获取小时数
	 * 
	 * @return
	 */
	public static String getHours()
	{
		String returnString = null;
		Calendar calendar = Calendar.getInstance();
		int hours = calendar.get(Calendar.HOUR_OF_DAY);
		if (hours < 10)
		{
			returnString = "0" + hours;
		}
		else
		{
			String.valueOf(hours);
		}
		return returnString;
	}

	/**
	 * 获取小时数
	 * 
	 * @param calendar
	 * @return
	 */
	public static String getHours(Calendar calendar)
	{
		String returnString = null;
		int hours = calendar.get(Calendar.HOUR_OF_DAY);
		if (hours < 10)
		{
			returnString = "0" + hours;
		}
		else
		{
			String.valueOf(hours);
		}
		return returnString;
	}

	/**
	 * 获取当前时间是一年中的第几天
	 * 
	 * @return
	 */
	public static int getDayOfYear()
	{
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 获取当前时间是一年中的第几天
	 * 
	 * @param calendar
	 * @return
	 */
	public static int getDayOfYear(Calendar calendar)
	{
		return calendar.get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 获取当前日期是当前月份中的第几周
	 * 
	 * @return
	 */
	public static int getWeekOfMonth()
	{
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);
	}

	/**
	 * 获取当前日期是当前月份中的第几周
	 * 
	 * @param calendar
	 * @return
	 */
	public static int getWeekOfMonth(Calendar calendar)
	{
		return calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);
	}

	/**
	 * 获取系统日期时间
	 * 
	 * @return 格式:yyyyMMdd HH:mm:ss
	 */
	public static String getSystemDateTime()
	{
		return convertDate2String(new Date(), dateTimePattern2);
	}

	/**
	 * 将日期格式转换为字段串,返回格式:yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 */
	public static String convertDate2String(Date date)
	{
		return convertDate2String(date, datePattern);
	}

	/**
	 * 将日期格式转换指定格式字段串
	 * 
	 * @param date
	 * @param patternString
	 * @return
	 */
	public static String convertDate2String(Date date, String patternString)
	{
		String returnString = null;
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(patternString);
		returnString = sdf.format(date);
		return returnString;
	}

	/**
	 * 将日期格式转换为日期时间字符串,返回字符串格式:yyyy-MM-dd HH:mm:ss
	 * 
	 * @param dateTime
	 * @return
	 */
	public static String convertDateTime2String(Date dateTime)
	{
		String returnString = null;
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(dateTimePattern);
		returnString = sdf.format(dateTime);
		return returnString;
	}

	/**
	 * 将字符串转换为日期格式
	 * 
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public static Date convertString2Date(String dateString) throws ParseException
	{
		Date returnDate = null;
		SimpleDateFormat sdf = new SimpleDateFormat();
		if (dateString.length() == 8)
		{
			sdf.applyPattern(datePattern_yyyyMMDD);
		}
		else if (dateString.length() == 10)
		{
			sdf.applyPattern(datePattern);
		}
		else if (dateString.length() >= 19)
		{
			sdf.applyPattern(dateTimePattern);
			dateString = dateString.substring(0, 19);
		}
		returnDate = sdf.parse(dateString);

		return returnDate;
	}

}
